package com.cmcc.qx.ws.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cmcc.qx.common.dao.impl.BaseDaoImpl;
import com.cmcc.qx.common.entity.Article;
import com.cmcc.qx.common.entity.BackBean;
import com.cmcc.qx.ws.dao.BackArtDao;

@Repository("backArtDaoImpl")
public class BackAtrDaoImpl extends BaseDaoImpl implements BackArtDao {

	public void addArt(Article atr) {
		this.save(atr);
	}

	public List<Article> queryArt(int start, int limit, BackBean bean) {
		String hql = "from Article where 1=1 and sts = 'A' ";
		if (!"".equals(bean.getAtrname())) {
			hql += "and title like '" + bean.getAtrname() + "'";
		}
		if (!"".equals(bean.getAtrtype())) {
			hql += "and type = '" + bean.getAtrtype() + "'";
		}
		if (!"".equals(bean.getStarttime())) {
			hql += "and createTime > '" + bean.getStarttime() + "'";
		}
		if (!"".equals(bean.getEndtime())) {
			hql += "and createTime < '" + bean.getEndtime() + "'";
		}
		List<Object> list = queryHql(start, limit, hql);
		List<Article> arts = new ArrayList<Article>();
		for (Object obj : list) {
			Article art = (Article) obj;
			arts.add(art);
		}
		return arts;
	}

	public int queryCnt(BackBean bean) {
		String sql = "";
		sql += "select count(*) from xqarticle where 1=1 and sts = 'A' ";
		if (!"".equals(bean.getAtrname())) {
			sql += "and title like '" + bean.getAtrname() + "'";
		}
		if (!"".equals(bean.getAtrtype())) {
			sql += "and type = '" + bean.getAtrtype() + "'";
		}
		if (!"".equals(bean.getStarttime())) {
			sql += "and createTime > '" + bean.getStarttime() + "'";
		}
		if (!"".equals(bean.getEndtime())) {
			sql += "and createTime < '" + bean.getEndtime() + "'";
		}
		return Integer.valueOf(this.querySql(sql).get(0).toString());
	}

	public void deleteArt(List<String> ids) {
		for(String id : ids){
			Article art = new Article();
			art.setId(Integer.valueOf(id));
			this.delete(art);
		}
	}

	public void hiddenArt(List<String> ids) {
		for(String id : ids){
			Article art = queryArticle(Integer.valueOf(id));
			art.setId(Integer.valueOf(id));
			art.setSts("H");
			this.update(art);
		}
	}
	
	public Article queryArticle(int id){
		String hql = " from Article where id = ?";
		return (Article) this.queryHql(hql, id).get(0);
	}

}
